import Config from "@/config/dev.js";
import { __token__name__ } from "@/config/storage.js";
// import { tansParams } from '@/utils'
let hasShowToLoginModal = false; // 是否弹出了去登录标识

const install = (Vue, vm) => {
  const http = uni.$u.http;

  // 此为自定义配置参数，具体参数见上方说明
  http.setConfig((config) => {
    config.baseURL = Config.requestUrl;
    return config;
  });
  http.interceptors.request.use(
    (config) => {
		console.log("发起请求：", config)
      const token = uni.getStorageSync(__token__name__); // 用户token
      // post请求封装请求方式
      if(config.method.toLocaleLowerCase() == 'post') {
        config.header['Content-type'] = config.header['Content-type'] || "application/x-www-form-urlencoded"; 
      }
      if (token) {
        config.header['Authorization'] = `${token}`;
      }
      return config;
    },
    (config) => {
      // 可使用async await 做异步操作
      return Promise.reject(config);
    }
  );

  // 响应拦截，判断状态码是否通过
  http.interceptors.response.use(
    (response) => {
      const { custom } = response.config;
	  console.log("请求成功返回：", response)
      if (custom.__keep__all__) {
        // 设计全部返回，调用地方自行处理
        return response.data;
      }
      if (response.statusCode === 200) {
        // 处理成功
        const { code } = response.data;

        if (code == 200 || code == 100) {
          return response.data.data ? response.data.data : response.data;
        }

        if (code == 401 || code == 10000) {
          // 确保只弹出一次弹窗
          if (hasShowToLoginModal) {
            return
          }
          uni.hideLoading();
          hasShowToLoginModal = true;
          uni.showModal({
            content: '亲，登录后获取更佳体验',
            showCancel: true,
            cancelText: '以后再说',
            confirmText: '前往登录',
            success: ret => {
              if (ret.confirm) {
                //前往统一登录页
                uni.redirectTo({
                  url: '/pages/login',
                })
              } else {
                // uni.navigateBack({
                // 	delta: 1
                // })
              }
              hasShowToLoginModal = false;
            }
          })
          return Promise.reject(response.data)
        }
      }
      setTimeout(() => {
        uni.showToast({
          title: response.message || response.data.message || "未知错误",
          icon: 'none',
          duration: 5000,
        })
      }, 100)
      return Promise.reject(response.data)

    }, response => {
		console.log("请求失败返回：", response)
      if (response.data.code === 401) {
        // 确保只弹出一次弹窗
        if (hasShowToLoginModal) {
          return
        }
        uni.hideLoading();
        hasShowToLoginModal = true;
        uni.showModal({
          content: '亲，登录后获取更佳体验',
          showCancel: true,
          cancelText: '以后再说',
          confirmText: '前往登录',
          success: ret => {
            if (ret.confirm) {
              //前往统一登录页
              uni.redirectTo({
                url: '/pages/login',
              })
            } else {
              // uni.navigateBack({
              // 	delta: 1
              // })
            }
            hasShowToLoginModal = false;
          }
        })
      }
      return Promise.reject(response.data)
    })
  Vue.prototype.$http = http
}

export default {
  install,
};
